using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Q.Framework.Extension;

namespace Q.Framework.WebCode
{
    public class CrudMasterPageBase : MasterPageBase
    {
        public event EventHandler Save;
        public event EventHandler DoAfterSave;
        public event EventHandler Delete;
        public event EventHandler Cancel;
        public event EventHandler New;
        public event EventHandler RedirectToSearchPage;

        public Panel PanelContent
        {
            get
            {
                return this.FindControlsByType<Panel>().First(c => c.ID == "pnlAutoFormContent");
            }
            set
            {
                Panel place = this.FindControlsByType<Panel>().First(c => c.ID == "pnlAutoFormContent");
                place = value;
            }
        }

        private Boolean showNewButton = true;
        public Boolean ShowNewButton
        {
            get { return showNewButton; }
            set { showNewButton = value; }
        }

        private Boolean showSaveButton = true;
        public Boolean ShowSaveButton
        {
            get { return showSaveButton; }
            set { showSaveButton = value; }
        }

        private Boolean showSaveAndInsertButton = true;
        public Boolean ShowSaveAndInsertButton
        {
            get { return showSaveAndInsertButton; }
            set { showSaveAndInsertButton = value; }
        }

        private Boolean showReturnButton = true;
        public Boolean ShowReturnButton
        {
            get { return showReturnButton; }
            set { showReturnButton = value; }
        }

        private Boolean showDeleteButton = true;
        public Boolean ShowDeleteButton
        {
            get { return showDeleteButton; }
            set { showDeleteButton = value; }
        }

        private Boolean showCancelButton = true;
        public Boolean ShowCancelButton
        {
            get { return showCancelButton; }
            set { showCancelButton = value; }
        }

        private void MessageRecording(Boolean redirectToSearch)
        {
            if (DisplayMessageNewRecording)
            {
                String page = GetPage();

                if (redirectToSearch && Session["PageSearch"] != null)
                {
                    page = Session["PageSearch"].ToString();
                    Session["PageSearch"] = null;
                }

                MessageBoxAndRedirect(MessageNewRecord, page);
            }
            //JavaScriptAdd.MessageBoxShowRedrect(this.Page, MessageNewRecord, GetPage());
        }

        private void MessageDeleteRecording()
        {
            if (DisplayMessageDeleteRecording)
            {
                if (DisplayMessageNewRecording)
                {
                    String page = GetPage();

                    if (Session["PageSearch"] != null)
                    {
                        page = Session["PageSearch"].ToString();
                        Session["PageSearch"] = null;
                    }

                    MessageBoxAndRedirect(MessageDeleteRecord, page);
                    //JavaScriptAdd.MessageBoxShowRedrect(this.Page, MessageDeleteRecord, GetPage());
                }
            }

        }

        protected void DoCancel(object sender, EventArgs e)
        {
            Cancel(sender, e);
        }

        protected void DoSave(object sender, EventArgs e)
        {
            Save(sender, e);
            if (!(Page.Request.QueryString != null && Page.Request.QueryString["FastCrud"] != null && Page.Request.QueryString["FastCrud"] == "S"))
                MessageRecording(true);
            if (DoAfterSave != null)
                DoAfterSave(sender, e);
        }

        protected void DoDelete(object sender, EventArgs e)
        {
            Delete(sender, e);
            MessageDeleteRecording();
        }

        protected void DoSaveAndInsertOther(object sender, EventArgs e)
        {
            Save(sender, e);
            MessageRecording(false);
        }

        protected void DoNew(object sender, EventArgs e)
        {
            New(sender, e);
        }

        protected void DoRedirectToSearchPage(object sender, EventArgs e)
        {
            RedirectToSearchPage(sender, e);
        }

        private String GetPage()
        {
            return this.Page.Request.Url.Segments.Last();
        }

        protected override void OnInit(EventArgs e)
        {
            if (Page.Request.QueryString != null && Page.Request.QueryString["FastCrud"] != null && Page.Request.QueryString["FastCrud"] == "S")
            {
                SetContentFullPage();
                ShowDeleteButton = false;
                ShowSaveAndInsertButton = false;
                ShowCancelButton = false;
                ShowNewButton = false;
                ShowReturnButton = false;
            }

            base.OnInit(e);
        }
    }
}